package com.jsu.springboot_crop.service.impl;

import com.jsu.springboot_crop.dao.BankDao;
import com.jsu.springboot_crop.dao.FinanceDao;
import com.jsu.springboot_crop.entity.*;
import com.jsu.springboot_crop.service.FinanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @Author wang
 * @Date 2023/8/25 19:42
 */
@Service
public class FinanceServiceImpl implements FinanceService {
    @Autowired(required = false)
    FinanceDao financeDao;

    @Override
    public void addFinance(Finance finance) {
        finance.setStatus(0);
        finance.setCreateTime(new Date());
        finance.setUpdateTime(new Date());
        financeDao.insertFinance(finance);
    }

    @Override
    public void addFinanceMulti(Finance finance) {
        finance.setStatus(0);
        finance.setCreateTime(new Date());
        finance.setUpdateTime(new Date());
        financeDao.insertFinanceMulti(finance);
    }

    @Override
    public List<Finance> selectByFinance(Finance finance) {
        return financeDao.selectByFinance(finance);
    }

    @Override
    public Finance selectByFinanceId(Integer id) {
        return financeDao.selectByPrimaryKey(id);
    }

    @Override
    public void updateByFinance(Finance finance) {
        finance.setUpdateTime(new Date());
        financeDao.updateByPrimaryKeySelective(finance);
    }

    @Override
    public void deleteByFinanceId(Integer id) {
        financeDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<Intention> selectIntentionByName(String name) {
        return financeDao.selectIntentionByName(name);
    }

    @Override
    public void updateIntentionByName(Intention intention) {
        intention.setUpdateTime(new Date());
        financeDao.updateIntentionByName(intention);
    }

    @Override
    public void insertIntentionByName(Intention intention) {
        intention.setCreateTime(new Date());
        intention.setUpdateTime(new Date());
        financeDao.insertIntentionByName(intention);
    }

    @Override
    public void deleteIntentionByName(String name) {
        financeDao.deleteIntentionByName(name);
    }

    @Override
    public List<Recommend> selectRecommend(String name) {
        Intention intention = financeDao.selectIntention(name);
        if (StringUtils.isEmpty(intention)) {//无融资意向
            List<Recommend> recommends = financeDao.selectAllRecommend(name);
            return recommends;
        } else {//有融资意向
            List<Recommend> recommends = financeDao.selectWithNoneIntention(intention.getItem(),intention.getId());
            System.out.println("item: "+intention.getItem()+"  id: "+intention.getId());
            return recommends;
        }
    }

    @Override
    public boolean selectIfApply(String name) {
        Finance finance = financeDao.selectIfApply(name);
        if(StringUtils.isEmpty(finance)){
            return false;
        }
        return true;
    }

}
